import React, { Component } from "react";
import { connect } from "react-redux";

const mapStateToProps = (state) => {
  return {
    // count: state.count,
    // list: state.list,
    count: state.get("count"),
    list: state.get("list"),
  };
};

const mapDispatchToProps = (dispatch) => {
  return {
    increment() {
      dispatch({ type: "increment" });
    },
    decrement() {
      dispatch({ type: "decrement" });
    },
    add(payload) {
      dispatch({ type: "add", payload });
    },
  };
};

@connect(mapStateToProps, mapDispatchToProps)
class App extends Component {
  state = {
    value: "",
  };
  handleChange = (e) => {
    this.setState({
      value: e.target.value,
    });
  };
  handleClick = () => {
    this.props.add({
      name: this.state.value,
      id: new Date().getTime(),
    });
  };
  render() {
    return (
      <>
        <h2>redux-immutable</h2>
        <button onClick={this.props.decrement}>-</button>
        <span>{this.props.count}</span>
        <button onClick={this.props.increment}>+</button>

        <hr />

        <input
          type="text"
          value={this.state.value}
          onChange={this.handleChange}
        />
        <button onClick={this.handleClick}>添加</button>
        <ul>
          {this.props.list.map((item) => (
            <li key={item.id}>{item.name}</li>
          ))}
        </ul>
      </>
    );
  }
}

export default App;
